/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.ktx;

import com.bytedance.scene.AbilityCompatibilityUtility;
import com.bytedance.scene.Scene;
import com.bytedance.scene.interfaces.PermissionResultCallback;
import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.ability.LifecycleStateObserver;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class SceneExtensions {
    private SceneExtensions() {
    }


    private static EventHandler getHANDLER() {
        return SceneExtensionsHandle.HANDLER;
    }

    public static class SceneExtensionsHandle{
        private static final EventHandler HANDLER =new EventHandler(EventRunner.getMainEventRunner());
    }


    public static void post(Scene scene, Runnable runnable) {
        if (scene.getLifecycle().getLifecycleState() == Lifecycle.Event.ON_STOP) {
            return;
        }

        getHANDLER().postTask(runnable);

        scene.getLifecycle().addObserver((LifecycleStateObserver) (event, intent) -> {
            if (Lifecycle.Event.ON_STOP.toString().equals(event.name())) {
                getHANDLER().removeTask(runnable);
            }
        });
    }

    public static void postDelayed(Scene scene, Runnable runnable, long delayTime) {
        if (scene.getLifecycle().getLifecycleState() == Lifecycle.Event.ON_STOP) {
            return;
        }

        getHANDLER().postTask(runnable, delayTime);

        scene.getLifecycle().addObserver((LifecycleStateObserver) (event, intent) -> {
            if (Lifecycle.Event.ON_STOP.toString().equals(event.name())) {
                getHANDLER().removeTask(runnable);
            }
        });
    }

    public static boolean isDestroyed(Scene scene) {
        return scene.getLifecycle().getLifecycleState() == Lifecycle.Event.ON_STOP;
    }

    public static Ability fragmentAbility(Scene scene) {
        return scene.getAbility();
    }

    public static Ability requireFragmentAbility(Scene scene) {
        return scene.requireAbility();
    }

    public static void requestPermissions(Scene scene, String[] permissions, int requestCode, PermissionResultCallback resultCallback){
        if(scene.getAbility()!=null){
            AbilityCompatibilityUtility.requestPermissions((FractionAbility) scene.getAbility(),
                    scene, permissions, requestCode, resultCallback);
        }
    }

    public static NavigationScene getNavigationScene(Scene scene){
        return NavigationSceneGetter.getNavigationScene(scene);
    }

    public static NavigationScene requireNavigationScene(Scene scene) {
        return NavigationSceneGetter.requireNavigationScene(scene);
    }

}
